/*
*   Copyright: Fabian Hajek (2012)  Programmieren: C   -   Semesterarbeit WS 2011/2012
*
*   Projekt: Schreinerei.exe
*
*   Author: $Author: webmaster@die-schwarzen-schwaben.de $
*
*   Name: func_save.c
*   URL:    $URL: http://semesterarbeit-wi-11-12.googlecode.com/svn/trunk/Semesterarbeit2/Schreinerei/Funktionen/func_save.c $
*
*   Description:   Beeinhaltet die Speicher-Funktione
*
*   Last update:    $Date: 2012-02-24 21:45:10 +0000 (Fri, 24 Feb 2012) $
*
*   Rev:      $Rev: 56 $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../default/structures.h"
#include "../Definitionen/define.h"
#include "func_save.h"

void save()
{
    int z;
    int f;
    FILE *pF;
    pF = fopen("temp.sv","w");
    if(pF!=NULL)
    {
        /*Aktuelle Ressources speichern*/
        fprintf(pF,"%s\n","[START RESSOURCES]");
        p_ressource = p_ressource_end;
        while(p_ressource)
        {
            fprintf(pF,"%s\n",p_ressource->name);
            fprintf(pF,"%f\n",p_ressource->price);
            fprintf(pF,"%c\n",p_ressource->entity);
            fprintf(pF,"%i\n",p_ressource->stock);
            p_ressource = p_ressource->prev_ressource;
        }
        fprintf(pF,"%s\n","[END RESSOURCES]");


        /*Aktuelle Arbeitsschritte speichern*/
        fprintf(pF,"%s\n","[START STEPS]");
        p_step = p_step_end;
        while(p_step)
        {
            fprintf(pF,"%s\n",p_step->name);
            /*Ressource (Pointer)*/
            p_ressource = p_ressource_end;
            f = 1;
            while(p_ressource)
            {
                if(p_ressource==p_step->p_ressources)
                {
                    f = 0;
                    fprintf(pF,"%s\n",p_ressource->name);
                }
                p_ressource = p_ressource->prev_ressource;
            }
            if(f)fprintf(pF,"\n");
            fprintf(pF,"%f\n",p_step->time);
            fprintf(pF,"%f\n",p_step->price);
            if(strcmp(p_step->ressource,""))fprintf(pF,"%s",p_step->ressource);
                fprintf(pF,"\n");
            p_step = p_step->prev_step;
        }
        fprintf(pF,"%s\n","[END STEPS]");

        /*Aktuelle Schraenke speichern*/
        fprintf(pF,"%s\n","[START SCHRAENKE]");
        p_schrank = p_schrank_end;
        while(p_schrank)
        {
            fprintf(pF,"%s\n",p_schrank->name);
            for(z=0;z<MAX_SCHRANK_RESOURCE;z++)
            {
                if(p_schrank->p_steps[z])
                {
                    p_step = p_schrank->p_steps[z];
                    fprintf(pF,"[STEP]\n");
                    fprintf(pF,"%s\n",p_step->name);
                    fprintf(pF,"%i\n",p_schrank->steps_quantity[z]);
                }
            }
            for(z=0;z<MAX_SCHRANK_RESOURCE;z++)
            {
                if(p_schrank->ressources_quantity[z])
                {
                    p_ressource = p_schrank->p_ressources[z];
                    fprintf(pF,"[RESSOURCE]\n");
                    fprintf(pF,"%s\n",p_ressource->name);
                    fprintf(pF,"%i\n",p_schrank->ressources_quantity[z]);
                }
            }
            p_schrank = p_schrank->prev_schrank;
        }
        fprintf(pF,"%s\n","[END SCHRAENKE]");

        fclose(pF);

        log("\nAktueller Stand wurde abgespeichert",0,1);
    }
}
